package Tree.BFS;

import Utils.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 得到二叉树左下角的节点
 * Lettcode 513: https://leetcode-cn.com/problems/find-bottom-left-tree-value/
 */
public class findBottomLeftValue {
    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int res = -1;
        while (!queue.isEmpty()) {
            int cnt = queue.size();
            for (int i = 0; i < cnt; i++) {
                TreeNode node = queue.poll();
                if (i == 0) {
                    res = node.val;
                }
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
        }
        return res;
    }
}
